

%MACRO score(in,data);

*****************************************************************************
* Program: SCORE					   						
* 																			
* Description: Scores interview instruments, performs range and logic checks,
* 				computes additional analytic vars, transforms selected vars.
*
*						
* Last modified: July 21, 2015												
* Created by: Martin Wegman													
*****************************************************************************;

%let dat = data;

data loc.&dat.1;
	set loc.&in;
run;

********************************************************************;
********************************************************************;
******************** DAST-10 scoring *******************************;
********************************************************************;
********************************************************************;

%let v = 1; %let v2 = %eval(&v + 1);
/*
proc freq data = loc.&dat&v;
	tables dast01 dast02 dast03 dast04 dast05 dast06 
			dast07 dast08 dast09 dast10 dast11 dast12 /missing;
run;
*/
data loc.&dat&v2;
	set loc.&dat&v;

	array x[*] dast01 - dast10;

	*Check that values are in range;
	do i=1 to 10;
		if x[i] not in (0,1,.) then do;
			put '************************';
			put '************************'; 
			put 'Out of range DAST, ID:' client_id 'value:' x[i]; 
			x[i] = .;
		end;
	end;
	
	*Compute DAST score and category;
	dast_score = 0;

	do i=1 to 10;

		if x[i] = . then dast_score = .;

		if i ~ = 3 then do;
			if x[i] = 1 then dast_score = dast_score + 1;
		end;
		else do;
			if x[i] = 0 then dast_score = dast_score + 1;
		end;

	end;
	
	if dast_score =. then dast_cat =.;
	else if dast_score = 0 then dast_cat = 0;
	else if dast_score in (1,2) then dast_cat = 1;
	else if dast_score in(3,4,5) then dast_cat = 2;
	else if dast_score in(6,7,8) then dast_cat = 3;
	else if dast_score in(9,10) then dast_cat = 4;

	format dast_cat dast_cat.;

	drop i dast11 dast12;

run;
/*
proc freq data = loc.&dat&v2;
	tables dast_score dast_cat /missing;
run;
*/
*********************************************************************;
*********************************************************************;
********************    MINI scoring ********************************;
*********************************************************************;
*********************************************************************;

%let v = 2; %let v2 = %eval(&v + 1);
/*
proc freq data = loc.&dat&v;
	tables od1-od9 od9a cs01_craving/missing;
run;

proc freq data = loc.&dat&v;
	tables oa1 oa2 oa3 oa4 oa5/missing;
run;

proc freq data = loc.&dat&v;
	where version = 1;
	tables od1a od1b od1c od1d od1e 
		od1f od1g od1h od1h1 od1h2 od1h3/missing;
run;
*/
data loc.&dat&v2;
	set loc.&dat&v;

	array x od1 - od7;
	array y od2 - od8;
	array a oa1 - oa4;

	*Different versions had different question numberings;
	if version = 2 then do; %let z = x; end;
	else do; %let z = y; end;

	od_count = 0;

	*Check that values are within range;
	do over &z;
		if &z not in (0,1,.) then do;
			put '************************';
			put '************************'; 
			put 'Out of range MINI (OD), ID:' client_id 'value:' &z; 
			&z = .;
		end;
		od_count = &z + od_count; *Calculate OD sum;
	end;

	do over a;
		if a not in (0,1,.) then do;
			put '************************';
			put '************************'; 
			put 'Out of range MINI (OA), ID:' client_id 'value:' a; 
			a = .;
		end;
	end;

	if cs01_craving not in(.,0,1,2,3,4,5,6,7,8,9,10) then do;
		put '************************';
		put '************************'; 
		put 'Out of range Cravings, ID:' client_id 'value:' cs01_craving; 
		cs01_craving = .; 
	end;

	*If not missing, calculate OD category;
	if od_count =. then od_cat =.;
	else if od_count > 2.1 then od_cat = 1;
	else od_cat = 0; 

	*Calculate OA sum and category for those who are not opiod dependent;
	if od_cat ~= 1 then do;

		oa_count = sum(of a[*]); 

		if oa_count =. then oa_cat = .;
		else if oa_count > 0.1 then oa_cat = 1;
		else oa_cat = 0; 

	end;

	else oa_cat = .;

	drop oa_count od_count;

run;
/*
proc freq data = loc.&dat&v2;
	tables od_cat/missing;
run;

proc freq data = loc.&dat&v2;
	where od_cat ~= 1;
	tables oa1 oa2 oa3 oa4 oa5 oa_cat/missing;
run;

proc freq data = loc.&dat&v2;
	tables cs01_craving/missing;
run;

*/
*********************************************************************;
*********************************************************************;
**************** SCORATES 8D scoring ********************************;
*********************************************************************;
*********************************************************************;

%let v = 3; %let v2 = %eval(&v + 1);
/*
proc freq data = loc.&dat&v;
	tables so1 - so19/missing;
run;
*/
data loc.&dat&v2;
	set loc.&dat&v;

	array x so1 - so19;

	do over x;
		if x not in (1,2,3,4,5,.) then do;
			put '************************';
			put '************************'; 
			put 'Out of range SOCRATES, ID:' client_id 'value:' x; 
			x = .;
		end;
	end;

	*Calculate raw subscale score;
	so_rec = so1 + so3 + so7 + so10 + so12 + so15 + so17;
	so_amb = so2 + so6 + so11 + so16;
	so_ts = so4 + so5 + so8 + so9 + so13 + so14 + so18 + so19;

	*Convert raw scores to decile scores;
	     if so_rec = 35 then so_rec_d = 70;
	else if so_rec = 34 then so_rec_d = 60;
	else if so_rec in (32,33) then so_rec_d = 50;
	else if so_rec = 31 then so_rec_d = 40;
	else if so_rec in (29,30) then so_rec_d = 30;
	else if so_rec in (27,28) then so_rec_d = 20;
	else if so_rec < 26.1 and so_rec > 6.9  then so_rec_d = 10;
	else so_rec_d = .;

		 if so_amb in(19,20) then so_amb_d = 90;
	else if so_amb = 18 then so_amb_d = 80;
	else if so_amb = 17 then so_amb_d = 70;
	else if so_amb = 16 then so_amb_d = 60;
	else if so_amb = 15 then so_amb_d = 50;
	else if so_amb = 14 then so_amb_d = 40;
	else if so_amb in (12,13) then so_amb_d = 30;
	else if so_amb in (9,10,11) then so_amb_d = 20;
	else if so_amb < 8.1 and so_rec > 3.9  then so_amb_d = 10;
	else so_amb_d = .;

		 if so_ts in(39,40) then so_ts_d = 90;
	else if so_ts in(37,38) then so_ts_d = 80;
	else if so_ts = 36 then so_ts_d = 70;
	else if so_ts in(34,35) then so_ts_d = 60;
	else if so_ts = 33 then so_ts_d = 50;
	else if so_ts in(31,32) then so_ts_d = 40;
	else if so_ts = 30 then so_ts_d = 30;
	else if so_ts in (26,27,28,29) then so_ts_d = 20;
	else if so_ts < 25.1 and so_rec > 7.9  then so_ts_d = 10;
	else so_ts_d = .;

run;
/*
proc freq data = loc.&dat&v2;
	tables so_rec so_amb so_ts so_rec_d so_amb_d so_ts_d
		/missing;
run;
*/
*********************************************************************;
****************    Drug treatment   ********************************;
*********************************************************************;
*********************************************************************;
%let v = 4; %let v2 = %eval(&v + 1);

*Distributions and logic checks;
/*
*Ever/current suboxone and methadone counts;
proc freq data = loc.&dat&v;
	tables dt01 dt02 dt01*dt02 dt01b dt01b*dt01
		dt02b dt02b*dt02/missing;
run;

proc freq data = loc.&dat&v;
	tables dt01 dt02 dt01*dt02 dt01b dt01b*dt01
		dt02b dt02b*dt02/missing;
run;
*** 26 ever given suboxone, 2  of which are current;
*** extra 3 say current suboxone, but missing suboxone ever;
*** 107 ever given methadone, 91 of which are current;
*** extra 672 say current methadone, but missing methadone ever???;

proc freq data = loc.&dat&v;
	tables 
			dt01c dt01d dt01e dt01f dt01g dt01h
			dt01i dt01j dt01k dt01l dt01m dt01m_d 
			dt02c dt02d dt02e dt02f dt02g dt02h
			dt02i dt02j dt02k dt02l dt02m dt02n 
			dt01ca dt02ca dt01d2_specify dt02d2_specify /missing;
run;
***dt02k - dt02n have many no's not many yes's;
***dt02j (satisfaction with treatment) is very high;
***dt02f (dosing) has good variation;
***dt02e (missed dose) has some good variation;

*Current suboxone treatment; 
*Comment out because suboxone counts too low;

proc freq data = loc.&dat&v;
	where dt01b = 1;
	tables dt01c dt01ca;
run;

proc means data = loc.&dat&v;
	class dt01b;
	var dt01c;
run;

proc freq data = loc.&dat&v;
	where dt01b = 1;
	tables 	dt01d dt01e dt01f dt01g dt01h
			dt01i dt01j dt01k dt01l dt01m dt01m_d 
			 dt01d2_specify /missing;
run;

*Current methadone treatment;
proc freq data = loc.&dat&v;
	tables dt02b*site dt02*site/missing;
run;
***At CnC at baseline - 83 of 97 (1 missing) endorse ever methadone, 83 of 97 (14 missing) are current;
***At Puspen at baseline - 24 of 98 (7 missing) endore ever methadone, 8 of 98 (74 missing) are current;

proc freq data = loc.&dat&v;
	where dt02b = 1;
	tables dt02c dt02ca;
run;
	
proc means data = loc.&dat&v;
	class dt02b;
	var dt02c;
run;

proc freq data = loc.&dat&v;
	where dt02b = 1 ; 
	tables 	dt02d dt02e dt02f dt02g dt02h
			dt02i dt02j dt02k dt02l dt02m dt02n 
			dt02d2_specify /missing;
run;
***At baseline, 8 current from Puspen 
	+ 83 current from CnC = 91 participants on methadone;
*** Almost all get MTD at C&C and large majority are very satisfied;
*/

*Range checks, cleaning;

data loc.&dat&v2;
	set loc.&dat&v;
	
	array x dt01 dt01b dt01e dt01k dt01l dt01m dt01m_d 
			dt02 dt02b dt02e dt02k dt02l dt02m dt02n 
			dt01ca dt02ca; 
			
	array y dt01g dt01h dt01i dt01j 
			dt02g dt02h dt02i dt02j;
			
	*Check that values are within range;
	do over x;
		if x not in (0,1,.) then do;
			put '************************';
			put '************************'; 
			put 'Out of range Drug Treat, ID:' client_id 'value:' x; 
			x = .;
		end;
	end;	

	do over y;
		if y not in (1,2,3,4,.) then do;
			put '************************';
			put '************************'; 
			put 'Out of range Drug Treat, ID:' client_id 'value:' y; 
			y = .;
		end;
	end;	
	
	if dt01d not in (1,2,3,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'dt01d' dt01d; 
		dt01d = .;
	end;
	
	if dt01f not in (0,1,2,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'dt01f' dt01f; 
		dt01f = .;
	end;
	
	if dt02d not in (1,2,3,4,5,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'dt02d' dt02d; 
		dt02d = .;
	end;
	
	if dt02f not in (0,1,2,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'dt02f' dt02f; 
		dt02f = .;
	end;
	
	if (dt01c >500 OR dt01c < 0) AND dt01c ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'dt01c' dt01c; 
		dt01c=.;
	end;
	
	if (dt02c > 500 OR dt02c < 0) AND dt02c ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'dt02c' dt02c; 
		dt02c=.;
	end;

	if DT01 = 0 then DT01b = 0;

	
run;

*********************************************************************;
****************   Quality of life   ********************************;
*********************************************************************;
*********************************************************************;

%let v = 5; %let v2 = %eval(&v + 1);
/*
proc freq data = loc.&dat&v;
	where version = 2;
	tables ql01-ql26 /missing;
run;
*/
data loc.&dat&v2;
	set loc.&dat&v;

	* Compute WHOQOL scores for new questionnaire;
	if version = 2 then do;

		array x[*] ql01 - ql26;
		m_count = 0;

		do i = 1 to 26;
			if x[i] > 5.1 or x[i] < 0.9 then x[i] = .;
			if x[i] = . then m_count = m_count + 1;
		end;

		array y[*] ql03 ql04 ql26;

		if m_count < 5.2 then do;

		do i = 1 to 3;
			if y[i] = 1 then y[i] = 5;
			else if y[i] = 2 then y[i] = 4;
			else if y[i] = 3 then y[i] = 3;
			else if y[i] = 4 then y[i] = 2;
			else if y[i] = 5 then y[i] = 1;
		end;

		if (missing(ql03) + missing(ql04) + missing(ql10) 
			+ missing(ql15) + missing(ql16)
			+ missing(ql17) + missing(ql18)) > 1.1 
			then ql_dom1 = .;
		else ql_dom1 = 
			mean(ql03,ql04,ql10,ql15,ql16,ql17,ql18)*4;

		if (missing(ql05) + missing(ql06) + missing(ql07) 
			+ missing(ql11) + missing(ql19)
			+ missing(ql26)) > 1.1 
			then ql_dom2 = .;
		else ql_dom2 = 
			mean(ql05,ql06,ql07,ql11,ql19,ql26)*4;

		if (missing(ql20) + missing(ql21) + missing(ql22)) > 1.1 
			then ql_dom3 = .;
		else ql_dom3 = 
			mean(ql20,ql21,ql22)*4;

		if (missing(ql08) + missing(ql09) + missing(ql12) 
			+ missing(ql13) + missing(ql14)
			+ missing(ql23) + missing(ql24)
			+ missing(ql25)) > 2.1 
			then ql_dom4 = .;
		else ql_dom4 = 
			mean(ql08,ql09,ql12,ql13,ql14,ql23,ql24,ql25)*4;

		end;

	end;
	
	drop i;

run;
/*
proc freq data = loc.&dat&v2;
	where version = 2;
	tables ql_dom1 - ql_dom4 /missprint;
run;

proc univariate data = loc.&dat&v2;
	where version = 2;
	var ql_dom1 - ql_dom4;
run;
*/
*********************************************************************;
****************   Health Outcomes   ********************************;
*********************************************************************;
*********************************************************************;

%let v = 6; %let v2 = %eval(&v + 1);

*Distributions and logic checks;
/*
proc freq data = loc.&dat&v;
	tables  he01 he02 he01*he02/missing;
run;
***At baseline, 164 had previous HIV test, 25 have not, 6 missing;

proc freq data = loc.&dat&v; * Should be none for he02 = 0 or . ; 
	tables	he02*(he01a he03 he03a he04) he03a*he04 he04*(he04a
			he05 he06 he06a he07 he07a he07b 
			he08 he09 he10 he10a he11 he12 he11a he13 
			he13a he13b he13c) he14 he14*(he14a he14b)/missing;
run;
***At baseline, 164 had previous HIV test, 25 have not, 6 missing;
***Of 164, 7 were positive, 12 did not find out status, 142 negative;
***Of 164, 6 were told they had HIV by provider (5 overlap with 7 reporting positive);
***5 saw doctors and all had CD4s in range 300-640 (2 below 500);
***2 told they needed to start ART and they started;

***At baseline, 19 experienced OD in past 6 months, 127 no, 49 missing;
***Most OD 1-3 times, although some with 5, 10, 20; 

proc freq data = loc.&dat&v;
	where he02 = 1;
	tables  he03a he04 he03a*he04/missing;
run;

proc freq data = loc.&dat&v;
	where he03a = 1 or he04 = 1;
	tables  he06 he08 he11 he12 he13 
			he13a he13b he13c/missing;
run;

proc freq data = loc.&dat&v;
	tables  he02*he14/missing;
run;

proc means data = loc.&dat&v;
	vars he01a he10 he14a he14b;
run;

*/
data loc.&dat&v2;
	set loc.&dat&v;
	
	array x he01 he02 he04 he06 he07 he07a he07b 
			he08 he11 he12 he13 he13a he13b he13c he14;
	
	*Check that values are within range;
	do over x;
		if x not in (0,1,.) then do;
			put '************************';
			put '************************'; 
			put 'Out of range Drug Treat, ID:' client_id 'value:' x; 
			x = .;
		end;
	end;
	
	if he03a not in (0,1,2,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'he03a:' he03a; 
		he03a = .;
	end;

	if he11a not in (0,1,2,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'he11a:' he11a; 
		he11a = .;
	end;
	
	if (he01a > 31 OR he01a < 0) AND he01a ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'he01a' he01a; 
		he01a=.;
	end;
	
	if (he10  < 0) AND he10  ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'he10 ' he10 ; 
		he10=.;
	end;
	
	if (he14a  < 0) AND he14a  ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'he14a ' he14a; 
		he14a=.;
	end;
	
	if (he14b  < 0) AND he14b  ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range Drug Treat, ID:' client_id 'he14b ' he14b; 
		he14b=.;
	end;
	
	if HE02 = 0 then HE03A = 3; * Was not tested;
	
run;


*********************************************************************;
*************    Social Support Scale   *****************************;
*********************************************************************;
*********************************************************************;


%let v = 7; %let v2 = %eval(&v + 1);
/*
proc freq data = loc.&dat&v;
	tables ss01 -- ss13 /missing;
run;
*/
data loc.&dat&v2;
	set loc.&dat&v;

	array x ss01 ss02 ss03 ss04 ss05 ss06
			ss07 ss08 ss09 ss10 ss11 ss12;
	
	*Check that values are in range;
	do over x;
		if x not in(.,1,2,3,4,5,6,7) then  do;
			put 'Out of range';
			put 'Out of range';
			put x;
			x = .;
		end;
	end;

	if ss13 not in(.,0,1) then  do;
		put 'Out of range';
		put 'Out of range';
		put ss13;
		ss13 = .;
	end;

	ss_total =  ss01 + ss02 + ss03 + ss04 + ss05 + ss06 +
				ss07 + ss08 + ss09 + ss10 + ss11 + ss12;
	ss_so = ss01 + ss02 + ss05 + ss10;
	ss_fam = ss03 + ss04 + ss08 + ss11;
	ss_fri = ss06 + ss07 + ss09 + ss12;

run;
/*
proc freq data = loc.&dat&v2;
	tables ss_total ss_so ss_fam ss_fri ss13/missing;
run;

proc univariate data = loc.&dat&v2;
	var ss_total ss_so ss_fam ss_fri;
run;

proc means data = loc.&dat&v2 N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	vars ss_total ss_so ss_fam ss_fri;
run;
*/
*********************************************************************;
*************       Risk Behaviors      *****************************;
*********************************************************************;
*********************************************************************;

%let v = 8; %let v2 = %eval(&v + 1);
/*
proc freq data = loc.&dat&v;
	tables rbd1 rbs1a /missing;
run;

proc freq data = loc.&dat&v;
	tables rbd1 rbs1a /missing;
run;
*** At baseline, 73 of 195 had ever injected;
*** At baseline, 41 of 195 had had sex in past 30 days;

proc means data = loc.&dat&v N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	where rbs1a = 1; 
	var rbs1b;
run;

proc freq data = loc.&dat&v;
	where rbd1 = 1;
	tables 	rbd11af___1 rbd11af___2 rbd11af___3
			rbd11af___4 rbd11af___5 rbd11af___6 
			rbd14 rbd14a rbd15 rbd16 rbd17 rbd18;
run;
*** most injecting with friends, drug dealers and strangers;

proc means data = loc.&dat&v N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	where rbd1 = 1;
	var		rbd2 rbd3 rbd4 rbd5 rbd6 
			rbd7 rbd7a rbd8 rbd8a rbd9 rbd10 
			rbd11 rbd12 rbd13 rbd14b;
run;
*** Range 14 to 48 for age of first inject;
*** 0 to 30 days, number of days used in last month;
*** 0 to 12 number of times inject per day;
*** 0 to 40 injection partners;

proc means data = loc.&dat&v N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	where rbs1a = 1 AND rbs1b > 0;
	var	rbs2 rbs3 rbs4 rbs5 rbs6 
		rbs7 rbs8 rbs9 rbs10 rbs11 rbs12;
run;
*** Disagreement between number who had no HIV+ partners and
number of times had sex with HIV positive person (rbs2 v rbs7);
*/
data loc.&dat&v2;
	set loc.&dat&v;

	****** Check that variables are in range *******;

	array pt2   rbd1 rbd14a rbd15 rbd16 rbd17 rbd18 rbs1a;

	array pt 	rbd2 rbd4 rbd5 rbd6 rbd7 rbd7a rbd8 
				rbd8a rbd9 rbd10 rbd11 rbd12 rbd13 
				rbs1b rbs2 rbs3 rbs4 rbs5 rbs6 rbs7 
				rbs8 rbs9 rbs10 rbs11 rbs12;

	do over pt;
		if pt < 0 and pt ~=. then do;
			put '************************';
			put '************************'; 
			put 'Out of range rbd, ID:' client_id 'value: ' pt; 
			pt = .;
		end;
	end;

	do over pt2;
		if pt2 not in(.,0,1) then do;
			put '************************';
			put '************************'; 
			put 'Out of range rbd, ID:' client_id 'value: ' pt2; 
			pt2 = .;
		end;
	end;

	if rbd14 not in(.,1,2,3,4,88) then do;
		put '************************';
		put '************************'; 
		put 'Out of range rbd14, ID:' client_id 'value: ' rbd14;  
		rbd14 = .;
	end;

	if (0 > rbd14b or rbd14b > 100) AND rbd14b ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range rbd14b, ID:' client_id 'value: ' rbd14b;  
		rbd14b = .;
	end;
	

	if (0 > rbd3 or rbd3 > 30) AND rbd3 ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range rbd3, ID:' client_id 'value: ' rbd3;  
		rbd3 = .;
	end;

	****** Perform logical checks *******;

	* If never injected drugs, rbd2 through rbd18 
		should be missing or 0;
	array x	rbd2 rbd3 rbd4 rbd5 rbd6 
			rbd7 rbd7a rbd8 rbd8a rbd9 rbd10 
			rbd11 rbd11a rbd11b rbd11c rbd11d
			rbd11e rbd11f rbd12 rbd13 rbd14 
			rbd14a rbd14b rbd15 rbd16 rbd17 
			rbd18;

	if rbd1 = 0 then do;
		do over x;
			if x not in (.,0) then do;
				put '************************';
				put '************************'; 
				put 'Should be (0,.) ID:' client_id 'value: ' x;
			end;
		end;
	end;

	else do;

		* Age of first injection must be < current age;
		if rbd2 > dm0 then do; 
			put '************************';
			put '************************'; 
			put 'ID:' client_id 
				'Age of first inject: ' rbd2 
				'Current age: ' dm0;
		end;

		* Check compution of past 30 days injections;
		rbd5_calc = rbd3*rbd4;
	
		if rbd5_calc ~= rbd5 then do;
			put '************************';
			put '************************'; 
			put 'ID:' client_id 
				'rbd5: ' rbd5 'rbd5_calc: ' rbd5_calc;
		end;

		* If 0 times injected in past 30 days, rbd6 through rbd18 
			should be missing or 0;
		array y	rbd6 rbd7 rbd7a rbd8 rbd8a rbd9 rbd10 
				rbd11 rbd11a rbd11b rbd11c rbd11d
				rbd11e rbd11f rbd12 rbd13 rbd14 
				rbd14a rbd14b rbd15 rbd16 rbd17 rbd18;
	
		if rbd5 = 0 or rbd5 =. then do;
			do over y;
				if y not in (.,0) then do;
					put '************************';
					put '************************'; 
					put 'Should be (0,.) ID:' client_id 'value: ' y;
				end;
			end;
		end;
	
		else do;

			* Must be proportion of total injections;
			if rbd6 > rbd5 then do;
				put '************************';
				put '************************'; 
				put 'ID:' client_id 
					'rbd6: ' rbd6 '> rbd5: ' rbd5;  
					rbd6 = .;
			end;

			* Must be proportion of total injections;
			if rbd7 > rbd5 then do;
				put '************************';
				put '************************'; 
				put 'ID:' client_id 
					'rbd7: ' rbd7 '> rbd5: ' rbd5;  
					rbd7 = .;
			end;

			* Must be proportion of total injections;
			if rbd7a > rbd5 then do;
				put '************************';
				put '************************'; 
				put 'ID:' client_id 
					'rbd7a: ' rbd7a '> rbd5: ' rbd5;  
					rbd7a = .;
			end;

			* Must be proportion of total injections;
			if rbd8 > rbd5 then do;
				put '************************';
				put '************************'; 
				put 'ID:' client_id 
					'rbd8: ' rbd8 '> rbd5: ' rbd5;  
					rbd8 = .;
			end;

			* Must be proportion of total injections;
			if rbd8a > rbd5 then do;
				put '************************';
				put '************************'; 
				put 'ID:' client_id 
					'rbd8a: ' rbd8a '> rbd5: ' rbd5;  
					rbd8a = .;
			end;

			* Must be proportion of total injections;
			if rbd9 > rbd5 then do;
				put '************************';
				put '************************'; 
				put 'ID:' client_id 
					'rbd9: ' rbd9 '> rbd5: ' rbd5;  
					rbd9 = .;
			end;

			* Must be proportion of injecting partners;
			if rbd11 > rbd10 then do;
				put '************************';
				put '************************'; 
				put 'ID:' client_id 
					'rbd11: ' rbd11 '> rbd10: ' rbd10;  
				rbd11 = .;
			end;

			* Check that numbers endorsing rbs1/rbs1a line up 
				with numbers endoring rbs1b/rbs1a and rbs6;

			* Make sure rbs2-5 and rbs7-12 are proporitons;

			* Compute proportions;

			rbd6p = rbd6 / rbd5;
			rbd7p = rbd7 / rbd5;
			rbd7ap = rbd7a / rbd5;
			rbd8p = rbd8 / rbd5;
			rbd8ap = rbd8a / rbd5;
			rbd9p = rbd9 / rbd5;
		end;

	end;

run;
/*
proc means data = loc.&dat&v2 N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	where rbd1 = 1;
	var		rbd2 rbd3 rbd4 rbd5;
run;

proc means data = loc.&dat&v2 N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	where rbd5 >0;
	var		rbd10 rbd11 rbd12 rbd13 rbd14b 
			rbd6p rbd7p rbd7ap rbd8p rbd8ap rbd9p;
run;
*/
*********************************************************************;
*************         Demographics      *****************************;
*********************************************************************;
*********************************************************************;

%let v = 9; %let v2 = %eval(&v + 1);
/*
proc freq data = loc.&dat&v;
	tables dm1 dm2 dm3 dm4 dm5 dm5_others/missing;
run;

proc means data = loc.&dat&v N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	var dm6a dm6b;
run;

proc contents data = loc.&dat&v; run;
*** dm6 dm7 dm8 are char type, need to convert;

proc freq data = loc.&dat&v;
	tables dm6 dm7 dm8/missing;
run;

proc means data = loc.&dat&v N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	var dm0;
run;

ods graphics on;
proc univariate data = loc.&dat&v all plots;
	var dm0;
run;
ods graphics off;

proc print data = loc.&dat&v;
	where ((dm0 < 18) or (dm0 > 60)) and dm0 ~=. ;
	var record_id client_id dm0;
run;

*/
data loc.&dat&v2;
	set loc.&dat&v (rename=(dm7 = _dm7 dm6 = _dm6 dm8 = _dm8));
	
	dm6 = _dm6*1;
	dm7 = _dm7*1;
	dm8 = _dm8*1;

	* Check that values are in range;
	if (18 > dm0 or dm0 > 100) AND dm0 ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm0, ID:' client_id 'dm0:' dm0;  
		dm0 = .;
	end;
	
	if dm1 NOT in(1,2,3,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm1, ID:' client_id 'dm1:' dm1;  
		dm1 = .;
	end;
	
	if dm2 NOT in(1,2,3,88,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm2, ID:' client_id 'dm2:' dm2;  
		dm2 = .;
	end;
	
	if dm3 NOT in(1,2,3,4,5,6,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm3, ID:' client_id 'dm3:' dm3;  
		dm3 = .;
	end;
	
	if dm4 NOT in(1,2,3,4,5,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm4, ID:' client_id 'dm4:' dm4;  
		dm4 = .;
	end;
	
	if dm5 NOT in(1,2,3,4,5,6,7,8,88,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm5, ID:' client_id 'dm5:' dm5;  
		dm5 = .;
	end;
	
	if (0 > dm6) AND dm6 ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm6, ID:' client_id 'dm6:' dm6;  
		dm6 = .;
	end;
	
	if (0 > dm6a) AND dm6a ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm6a, ID:' client_id 'dm6a:' dm6a;  
		dm6a = .;
	end;
	
	if (0 > dm6b) AND dm6b ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm6b, ID:' client_id 'dm6b:' dm6b;  
		dm6b = .;
	end;
	
	if (0 > dm7 OR dm7 > 99) AND dm7 ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm7, ID:' client_id 'dm7:' dm7;  
		dm7 = .;
	end;
	
		
	if (0 > dm8 OR dm8 > 99) AND dm8 ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range dm8, ID:' client_id 'dm8:' dm8;  
		dm8 = .;
	end;
	
	* Logic check;
	
	dm6_sum = dm6a + dm6b;
	
	/*if dm6_sum ~= dm6 then do;
		put '************************';
		put '************************'; 
		put 'dm6 does not equal sum of dm6a and dm6b' client_id;  
	end;*/ 
	***Commented out because too many flags;

	dm6_m = dm6;
	if dm6_m > 8000 then dm6_m = 8000;

	dm6a_m = dm6a;
	if dm6a_m > 8000 then dm6a_m = 8000;

	dm6b_m = dm6b;
	if dm6b_m > 8000 then dm6b_m = 8000;

	drop _dm6 _dm7 _dm8;
	
run;
/*
proc means data = loc.&dat&v2 N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	var dm6 dm6a dm6b dm7 dm8;
run;

proc means data = loc.&dat&v2 N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX;
	var dm0;
run;

*/
*********************************************************************;
***       Detention and Incarceration Hx        *********************;
*********************************************************************;
*********************************************************************;

%let v = 10; %let v2 = %eval(&v + 1);
/*
ods graphics on;
proc univariate data = loc.&dat&v plots;
	var ih01_prison ih02_lockup ih03_years 
		ih04_agefirsttime ih05_totaltimes ih06_totalyearsmonths;
run;
ods graphics off;
*** ih03_months  is char type;
*** considerable skewness in variables other than age of first detention;

proc print data = loc.&dat&v;
	where ih04_agefirsttime < 10 and ih04_agefirsttime ~=.;
	var record_id client_id ih04_agefirsttime;
run;

proc freq data = loc.&dat&v;
	tables ih03_months/missing;
run;
*/
data loc.&dat&v2;
	set loc.&dat&v (rename = (ih03_months = _ih03_months));
	
	ih03_months = _ih03_months*1;

	array y ih01_prison ih02_lockup ih05_totaltimes;
	
	*Check that values are in range;
	do over y;
		if (y <0 or y>999) AND y ~=. then do;
			put '************************';
			put '************************'; 
			put 'Out of range, ID:' client_id 'value:' y;
			y =.;
		end;
	end;
	
	if (ih03_years <0 or ih03_years>100) AND ih03_years ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range, ID:' client_id 'ih03_years:' ih03_years;
		ih03_years =.;
	end;
	
	if (ih03_months  <0 or ih03_months >12) AND ih03_months  ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range, ID:' client_id 
			'ih03_months :' ih03_months
			'ih03_years :' ih03_years; 
		ih03_months  =.;
	end;
	
	if (ih04_agefirsttime <5 or ih04_agefirsttime>100) AND ih04_agefirsttime~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range, ID:' client_id 'ih04_agefirsttime:' ih04_agefirsttime;
		ih04_agefirsttime =.;
	end;
	
	if (ih06_totalyearsmonths <0) AND ih06_totalyearsmonths ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range, ID:' client_id 'ih06_totalyearsmonths:' ih06_totalyearsmonths;
		ih06_totalyearsmonths =.;
	end;
	
	*Create new vars;
	ih03_totalmonths = ih03_years*12 + ih03_months;
	
	*Logic checks;
	
	if ih04_agefirsttime > dm0 AND dm0  ~=. then do;
		put '************************';
		put '************************'; 
		put 'ID:' client_id 
			'ih04_agefirsttime' ih04_agefirsttime '> age' dm0;  
		ih04_agefirsttime= .;
	end;
	
	age_months = dm0*12;
	
	if ih03_totalmonths > age_months AND age_months  ~=. then do;
		put '************************';
		put '************************'; 
		put 'ID:' client_id 
			'ih03_totalmonths' ih03_totalmonths '> age_months' age_months;  
		ih03_totalmonths= .;
	end;
	
	if ih06_totalyearsmonths > age_months AND age_months  ~=. then do;
		put '************************';
		put '************************'; 
		put 'ID:' client_id 
			'ih06_totalyearsmonths' ih06_totalyearsmonths '> age_months' age_months;  
		ih06_totalyearsmonths= .;
	end;

	ih06_totalyearsmonths_m = ih06_totalyearsmonths;
	if ih06_totalyearsmonths_m > 120 then ih06_totalyearsmonths_m = 120;

	drop _ih03_months;
run;


*********************************************************************;
*************         Drug Use          *****************************;
*********************************************************************;
*********************************************************************;

%let v = 11; %let v2 = %eval(&v + 1);
/*
proc freq data = loc.&dat&v;
	tables
		du01a_alcohol
		du02a_heroin
		du03a_morphine
		du04a_methadone
		du05a_sub
		du05aa_opium
		du06a_benzos
		du07a_syabu
		du08a_ecstasy
		du09a_pilkuda
		du011a_cannabis
		du012a_ketamine
		du013a_more
		du14_others
		du15_choice;
run;

proc freq data = loc.&dat&v;
	tables
		du14a_others_specify
		du14b_others_specify
		du14c_others_specify
		du14d_others_specify
		du3a_others;
run;	

proc contents data = loc.&dat&v;
run;
*/

/*

proc means data = loc.&dat&v N NMISS STD MEAN MIN Q1 MEDIAN Q3 MAX NOLABELS fw=5;
	var 
		du01b_alcohol_years
		du01c_alcohol_days
		du02b_heroin_years
		du02c_heroin_usedays
		du02d_heroin_injectdays
		du03b_morphine_years
		du03c_morphine_usedays
		du03d_morphine_injectdays
		du04b_methadone_years
		du04c_methadone_usedays
		du04d_methadone_injectdays
		du05b_sub_years
		du05c_sub_usedays
		du05d_sub_injectdays
		du05ab_opium_years
		du05ac_opium_usedays
		du05ad_opium_injectdays
		du06b_benzos_years
		du06c_benzos_usedays
		du06d_benzos_injectdays
		du07b_syabu_years
		du07c_syabu_usedays
		du07d_syabu_injectdays
		du08b_ecstasy_years
		du08c_ecstasy_usedays
		du08d_ecstasy_injectdays
		du09b_pilkuda_years
		du09c_pilkuda_usedays
		du09d_pilkuda_injectdays
		du011b_cannabis_years
		du011c_cannabis_usedays
		du011d_cannabis_injectdays
		du012b_ketamine_years
		du012c_ketamine_usedays
		du012d_ketamine_injectdays
		du013b_more_years
		du013c_more_usedays
		du013d_more_injectdays
		du16_agefirstdrug;
run;

*/
data loc.&dat&v2;
	set loc.&dat&v;
	
	array y du01a_alcohol
			du02a_heroin
			du03a_morphine
			du04a_methadone
			du05a_sub
			du05aa_opium
			du06a_benzos
			du07a_syabu
			du08a_ecstasy
			du09a_pilkuda
			du011a_cannabis
			du012a_ketamine
			du013a_more;
			
	array x du01b_alcohol_years
			du02b_heroin_years
			du03b_morphine_years
			du04b_methadone_years
			du05b_sub_years
			du05ab_opium_years
			du06b_benzos_years
			du07b_syabu_years
			du08b_ecstasy_years
			du09b_pilkuda_years
			du011b_cannabis_years
			du012b_ketamine_years
			du013b_more_years;
			
	array z
			du01c_alcohol_days
			du02c_heroin_usedays
			du02d_heroin_injectdays
			du03c_morphine_usedays
			du03d_morphine_injectdays
			du04c_methadone_usedays
			du04d_methadone_injectdays
			du05c_sub_usedays
			du05d_sub_injectdays
			du05ac_opium_usedays
			du05ad_opium_injectdays
			du06c_benzos_usedays
			du06d_benzos_injectdays	
			du07c_syabu_usedays
			du07d_syabu_injectdays
			du08c_ecstasy_usedays
			du08d_ecstasy_injectdays
			du09c_pilkuda_usedays
			du09d_pilkuda_injectdays
			du011c_cannabis_usedays
			du011d_cannabis_injectdays
			du012c_ketamine_usedays
			du012d_ketamine_injectdays
			du013c_more_usedays
			du013d_more_injectdays;
			
	array z1[*] du02c_heroin_usedays
			du03c_morphine_usedays
			du04c_methadone_usedays
			du05c_sub_usedays
			du05ac_opium_usedays
			du06c_benzos_usedays
			du07c_syabu_usedays
			du08c_ecstasy_usedays
			du09c_pilkuda_usedays
			du011c_cannabis_usedays
			du012c_ketamine_usedays	
			du013c_more_usedays;
			
	array z2[*] du02d_heroin_injectdays
			du03d_morphine_injectdays
			du04d_methadone_injectdays
			du05d_sub_injectdays
			du05ad_opium_injectdays
			du06d_benzos_injectdays	
			du07d_syabu_injectdays
			du08d_ecstasy_injectdays
			du09d_pilkuda_injectdays
			du011d_cannabis_injectdays
			du012d_ketamine_injectdays
			du013d_more_injectdays;

	array z3[*] du02a_heroin
			du03a_morphine
			du04a_methadone
			du05a_sub
			du05aa_opium
			du06a_benzos
			du07a_syabu
			du08a_ecstasy
			du09a_pilkuda
			du011a_cannabis
			du012a_ketamine
			du013a_more;
		
			
	*Check that values are in range;

	if (du16_agefirstdrug < 5) AND du16_agefirstdrug ~=. then do;
		put '************************';
		put '************************'; 
		put 'Out of range, ID:' client_id 'du16_agefirstdrug:' du16_agefirstdrug;
		du16_agefirstdrug =.;
	end;	

	do over y;
		if y NOT in(0,1,.) then do;
			put '************************';
			put '************************'; 
			put 'Out of range, ID:' client_id 'value:' y;
			y =.;
		end;
	end;
	
	do over x;
		if x ~=. AND (x <0 OR x > 100) then do;
			put '************************';
			put '************************'; 
			put 'Out of range, ID:' client_id 'x:' x;
			x =.;
		end;
	end;
	
	do over z;
		if z ~=. AND (z <0 OR z > 30) then do;
			put '************************';
			put '************************'; 
			put 'Out of range, ID:' client_id 'z:' z;
			if z > 30 and z <40 then z = 30; else z =.;
		end;
	end;
	
	if du14_others NOT in(0,1,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range, ID:' client_id 'du14_others:' du14_others;
		du14_others =.;
	end;
		
	if du15_choice NOT in(1,2,3,4,5,6,7,8,9,10,11,13,14,88,.) then do;
		put '************************';
		put '************************'; 
		put 'Out of range, ID:' client_id 'du15_choice:' du15_choice;
		du15_choice =.;
	end;
	
	* Logic checks;
	
	do over x;
		if x > dm0 AND x ~=. AND dm0 ~=. then do;
			put '************************';
			put '************************'; 
			put 'x > age' client_id 'value:' x 'age:' dm0;
			x =.;
		end;
	end;
	
	do i = 1 to 12;
		if z2[i] > z1[i] AND z1[i] ~=. then do;
			put '************************';
			put '************************'; 
			put 'inject days more than use days' z2[i]= z1[i]= client_id;
			z2[i]=.; z1[i]=.;
		end;
	end;
	
	
	if du16_agefirstdrug > dm0 then do;
		put '************************';
		put '************************'; 
		put 'ID:' client_id 
			'du16_agefirstdrug' du16_agefirstdrug '> age' dm0;  
		du16_agefirstdrug= .;
	end;

	do i = 1 to 12;
		if z3[i] = 0 then do;
			z2[i] = 0;
			z1[i] = 0;
		end;
	end;

	if du01a_alcohol = 0 then do;
		du01b_alcohol_years = 0;
		du01c_alcohol_days = 0;
	end;
	
	drop i;

run;

%let v = 12; %let v2 = %eval(&v + 1);
/*
proc freq data = loc.&dat&v;
	where du02c_heroin_usedays < 20;
	tables du02c_heroin_usedays;
run;
*/
%let limit = 1; * Threshold of days in past month need to use a drug 
					for it to qualify as recent polyuse;

data loc.&dat&v2;
	set loc.&dat&v;

	* Create new drug use vars;

	* All have used heroin;
	lifetime_polyuse = 	du03a_morphine 	OR 
						du05aa_opium 	OR
						du06a_benzos 	OR
						du07a_syabu 	OR
						du08a_ecstasy 	OR
						du09a_pilkuda 	OR
						du011a_cannabis OR
						du012a_ketamine OR
						du013a_more;

	recent_polyuse 		= lifetime_polyuse AND 
						((	du02c_heroin_usedays 	> &limit +
							du03c_morphine_usedays 	> &limit +
							du05ac_opium_usedays	> &limit +
							du06c_benzos_usedays	> &limit +
							du07c_syabu_usedays		> &limit +
							du08c_ecstasy_usedays	> &limit +
							du09c_pilkuda_usedays	> &limit +
							du011c_cannabis_usedays	> &limit +
							du012c_ketamine_usedays	> &limit) > 1);

			if 	du02c_heroin_usedays		=30 then 	daily_heroin 		= 1; 
	else 	if 	du02c_heroin_usedays		~=. then 	daily_heroin 		= 0;

			if 	du02c_heroin_usedays		>=15 then 	frequent_heroin 	= 1; 
	else 	if 	du02c_heroin_usedays		~=.	 then 	frequent_heroin 	= 0;

			if 	du013c_more_usedays			>=15 then 	frequent_moreuse 	= 1; 
	else 	if 	du013c_more_usedays			~=.	 then 	frequent_moreuse 	= 0;

			if 	du02c_heroin_usedays		>0	 then 	recent_heroin 		= 1; 
	else 	if 	du02c_heroin_usedays		~=.	 then 	recent_heroin 		= 0;

			if 	du01c_alcohol_days			>0	 then 	recent_alcohol 		= 1; 
	else 	if 	du01c_alcohol_days			~=.	 then 	recent_alcohol 		= 0;

			if 	du03c_morphine_usedays		>0 	OR 
				du05ac_opium_usedays		>0	OR
				du05d_sub_injectdays		>0	OR
				du04d_methadone_injectdays 	>0	then 	recent_other_opiates= 1;
	else 	if 	du03c_morphine_usedays		=. 	AND 
				du05ac_opium_usedays		=.	AND
				du05d_sub_injectdays		=.	AND
				du04d_methadone_injectdays 	=.	then 	recent_other_opiates= .;
	else												recent_other_opiates= 0;

			if 	du06c_benzos_usedays		>0 	then 	recent_benzos 		= 1; 
	else 	if 	du06c_benzos_usedays		~=.	then 	recent_benzos 		= 0;

			if 	du07c_syabu_usedays 		>0	OR
				du08c_ecstasy_usedays 		>0	OR
				du09c_pilkuda_usedays		>0	then 	recent_stimulants  	= 1;
	else 	if 	du07c_syabu_usedays 		=.	AND
				du08c_ecstasy_usedays 		=.	AND
				du09c_pilkuda_usedays		=.	then 	recent_stimulants  	= .;
	else												recent_stimulants	= 0;

			if 	du011c_cannabis_usedays		>0 	then 	recent_thc	 		= 1; 
	else 	if 	du011c_cannabis_usedays		~=.	then 	recent_thc 			= 0;

			if 	du013c_more_usedays			>0 	then 	recent_moreuse		= 1; 
	else 	if 	du013c_more_usedays			~=.	then 	recent_moreuse 		= 0;

			if 	du01a_alcohol				=1 	then 	lifetime_alcohol	= 1; 
	else 	if 	du01a_alcohol				=0	then 	lifetime_alcohol 	= 0;

			if 	du03a_morphine				=1 	OR 
				du05aa_opium				=1	OR
				du05d_sub_injectdays 		>0 	OR
				du04d_methadone_injectdays 	>0	then 	lifetime_other_opiates = 1;
	else 	if 	du03a_morphine				=. 	AND 
				du05aa_opium				=.	AND
				du05d_sub_injectdays 		=. 	AND
				du04d_methadone_injectdays 	=.	then 	lifetime_other_opiates= .;
	else												lifetime_other_opiates= 0;

			if 	du06a_benzos				=1 	then 	lifetime_benzos		= 1; 
	else 	if 	du06a_benzos				=0	then 	lifetime_benzos	 	= 0;

			if 	du07a_syabu 				=1	OR
				du08a_ecstasy 				=1	OR
				du09a_pilkuda				=1	then 	lifetime_stimulants = 1;
	else 	if 	du07a_syabu 				=.	AND
				du08a_ecstasy 				=.	AND
				du09a_pilkuda				=.	then 	lifetime_stimulants = .;
	else												lifetime_stimulants	= 0;

			if 	du011a_cannabis				=1 	then 	lifetime_thc		= 1; 
	else 	if 	du011a_cannabis				=0	then 	lifetime_thc	 	= 0;

			if 	du013a_more					=1 	then 	lifetime_moreuse	= 1; 
	else 	if 	du013a_more					=0	then 	lifetime_moreuse 	= 0;

run;

/*
proc freq data = loc.&dat&v2;
	tables daily_heroin lifetime: recent: freq: recent_polyuse*recent_moreuse/missing;
run;


proc freq data = loc.&dat&v2;
	tables lifetime_alcohol*recent_alcohol 	du01a_alcohol*	du01c_alcohol_days/missing;
run;

*/

%let v = 13; %let v2 = %eval(&v + 1);

* Transform variables for the models:;
data loc.&dat&v2;
	set loc.&dat&v;
	
			if dm2 in(1) 				then dm2_m = 1;
	else 	if dm2 in(3) 				then dm2_m = 3;
	else 	if dm2 in(2,88) 			then dm2_m = 2;

			if dm3 in(1,2,3) 			then dm3_m = 0;
	else 	if dm3 in(4,5,6) 			then dm3_m = 1;

			if dm4 in(1,3,4,5) 			then dm4_m = 0;
	else 	if dm4 in(2) 				then dm4_m = 1;

			if dm5 in(1) 				then dm5_m = 1;
	else 	if dm5 in(2,3,4,5,6,7,8,88)	then dm5_m = 2;

			if du15_choice in(2) 		then du15_choice_m = 2;
	else 	if du15_choice in(1,3,4,5,6,7,8,9,10,11,12,13,88) then du15_choice_m = 1;

			if dast_cat in(0) 			then dast_cat_m = 0;
	else 	if dast_cat in(1,2) 		then dast_cat_m = 2;
	else 	if dast_cat in(3) 			then dast_cat_m = 3;
	else 	if dast_cat in(4) 			then dast_cat_m = 4;

run;

/*examine/transform continuous variables */
/*
%let model_num = 	dm0
					ih01_prison 
					ih02_lockup 
					ih05_totaltimes 
					du16_agefirstdrug
					du02b_heroin_years 
					cs01_craving
					so_rec_d so_amb_d so_ts_d
					ss_so ss_fam ss_fri;

%let model_char = 	dm2_m dm3_m dm4_m dm5_m 
					du15_choice_m rbd1 
					dast_cat_m od_cat
					dt01 dt01b
					he01 he02 he03a
					daily_heroin 
					lifetime_polyuse 
					lifetime_alcohol 
					lifetime_other_opiates 
					lifetime_benzos 
					lifetime_stimulants 
					lifetime_thc 
					lifetime_moreuse 
					recent_polyuse 
					recent_heroin 
					recent_other_opiates 
					recent_benzos 
					recent_stimulants
					recent_moreuse 
					frequent_heroin 
					frequent_moreuse;

*excluded recent_alcohol recent_thc;

%MACRO hist_plots(v,list);
	%local i;
	%let i=1;
	%put &list=;
	%do %while("%scan(&list,&i)" NE "");
		%let var = %scan(&list,&i);
		%put &var=;
		proc sgplot data = loc.&dat&v;
			histogram &var;density &var;
		run;
		%let i = %eval(&i + 1);
	%end;

%MEND hist_plots;

%hist_plots(&v2,&model_num);

proc means data = loc.&dat&v2 N NMISS MEAN STD MIN Q1 MEDIAN Q3 MAX;
	var &model_num;
run;

ods graphics on;
proc univariate data = loc.&dat&v2 plots;
	var &model_num;
run;
ods graphics off;

proc freq data = loc.&dat&v2;
	tables 	&model_char
			/missing;
run;

* Make a list of other vars which have little missing to use for MI;

%let maybe_num =  	dm6a 
					dm6b 
					dm6 
					dm7 
					dm8
					ih03_totalmonths 
					ih04_agefirsttime 
					ih06_totalyearsmonths;

%let maybe_char =	rbs1a;

* Investigate maybes;

%hist_plots(&v2,&maybe_num);

proc means data = loc.&dat&v2 N NMISS MEAN STD MIN Q1 MEDIAN Q3 MAX;
	var &maybe_num;
run;

proc univariate data = loc.&dat&v2;
	var &maybe_num;
run;

proc freq data = loc.&dat&v2;
	tables &maybe_char;
run;

*/

%let v = 14;

data loc.&out;
	set loc.&dat&v;
	* attempt to transform highy skewed vars:
	ih01, ih02, ih05, ih06, du16, so_ts_d, ss_fam;

	ih01_sqrt = sqrt(ih01_prison);
	ih02_log = log(1 + ih02_lockup);
	ih06_sqrt = sqrt(ih06_totalyearsmonths);
	du16_log = log(1 + du16_agefirstdrug);

	*ih05,ss_fam and so_ts_d did not transform well;

	/*so_ts_d_sqrt = sqrt(100 - so_ts_d);
	so_ts_d_log = log(100 - so_ts_d);
	so_ts_d_inv = 1/(100 - so_ts_d);

	ss_fam_sqrt = sqrt(26 - ss_fam);
	ss_fam_log = log(26 - ss_fam);
	ss_fam_inv = 1/(26 - ss_fam);

	ih05_sqrt = sqrt(ih05_totaltimes);
	ih05_log = log(1 + ih05_totaltimes);
	ih05_inv = 1/(1 + ih05_totaltimes);
	*/

run;

/*

%let x = ;

proc sgplot data = loc.&dat&v2;
	histogram &x._sqrt; density  &x._sqrt;
	histogram &x._log; density  &x._log;
	histogram &x._inv; density  &x._inv;
run;
*/

****************************************************************************;
****************************************************************************;
************          Delete working datasets             ******************;
****************************************************************************;
****************************************************************************;

proc datasets lib=loc nolist;       
	delete &dat.1 - &dat.14;
	run;
quit;

****************************************************************************;
****************************************************************************;
**************                END                ***************************;
****************************************************************************;
****************************************************************************;

%MEND score;
